//
//  CouponManager.m
//  EasyToo
//
//  Created by zhengzhe on 13-8-25.
//
//

#import "CouponManager.h"
#import "ServerCommunicationManager.h"

#import "CouponSendRequest.h"
#import "CouponSendResponse.h"

#import "SearchCouponRequest.h"
#import "SearchCouponResponse.h"

#import "CouponMerchantRequest.h"
#import "CouponMerchantResponse.h"

#import "CouponShopRequest.h"
#import "CouponShopResponse.h"

#import "CouponDetailRequest.h"
#import "CouponDetailResponse.h"

@implementation CouponManager

static CouponManager* gManager = nil;


+ (CouponManager*)shareManager
{
    if (gManager == nil) {
        gManager = [[CouponManager alloc] init];
    }
    return gManager;
}

- (void)getMerchants
{
    CouponMerchantRequest *request = [[CouponMerchantRequest alloc] initWithPage:1 Size:40 Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
    [request release];
}

- (void)getShops:(NSString *)merchantId
{
    CouponShopRequest *request = [[CouponShopRequest alloc] initWithMerchantId:merchantId Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
    [request release];
}

- (void)getCoupons:(NSString *)shopId
{
    CouponDetailRequest *request = [[CouponDetailRequest alloc] initWithShopId:shopId Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
    [request release];
}

//搜索优惠券列表
- (void)searchCouponList
{
    SearchCouponRequest *request = [[SearchCouponRequest alloc] initWithKeyWord:@"" PageIndex:0 PageNum:20 Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
    [request release];
}

//发送优惠券
- (void)sendCoupon:(NSString*)couponId
            telNum:(NSString*)telNum
{
    CouponSendRequest *request = [[CouponSendRequest alloc] initWithTelNum:telNum CouponId:couponId Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
    [request release];
}

- (void)onRequestSuccess:(BaseResponse *)response
{
    if ([response.request isKindOfClass:[CouponSendRequest class]])
    {
        [self.delegate sendCouponFinish:YES];
    }
    else if ([response isKindOfClass:[CouponMerchantResponse class]])
    {
        CouponMerchantResponse *merchantResponse = (CouponMerchantResponse*)response;
        [self.delegate getMerchantsFinish:YES merchantArray:merchantResponse.merchantArray];
    }
    else if ([response isKindOfClass:[CouponShopResponse class]])
    {
        CouponShopResponse *shopResponse = (CouponShopResponse*)response;
        [self.delegate getShopsFinish:YES shopArray:shopResponse.shopArray];
    }
    else if ([response isKindOfClass:[CouponDetailResponse class]])
    {
        CouponDetailResponse *couponDetailResponse = (CouponDetailResponse*)response;
        [self.delegate getCouponsFinish:YES couponArray:couponDetailResponse.couponArray];
    }
    
}



- (void)onRequestFail:(BaseResponse *)response
{
    if ([response.request isKindOfClass:[CouponSendRequest class]])
    {
        [self.delegate sendCouponFinish:NO];
    }
    else if ([response isKindOfClass:[CouponMerchantResponse class]])
    {
        [self.delegate getMerchantsFinish:NO merchantArray:nil];
    }
    else if ([response isKindOfClass:[CouponShopResponse class]])
    {
        [self.delegate getShopsFinish:NO shopArray:nil];
    }
    else if ([response isKindOfClass:[CouponDetailResponse class]])
    {
        [self.delegate getCouponsFinish:NO couponArray:nil];
    }
}


@end

